package com.csxy.news.servlet;

import java.io.IOException;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import com.csxy.news.bean.News;
import com.csxy.news.service.AdminService;
import com.csxy.news.util.Pager;
import com.csxy.news.util.WebUtil;

/**
 * Servlet implementation class AdminServlet
 */
@WebServlet("/admin.servlet")
public class AdminServlet extends HttpServlet {
	private static final long serialVersionUID = 1L;
       
    /**
     * @see HttpServlet#HttpServlet()
     */
    public AdminServlet() {
        super();
        // TODO Auto-generated constructor stub
    }

	/**
	 * @see HttpServlet#service(HttpServletRequest request, HttpServletResponse response)
	 */
	protected void service(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		
		//检测是否为登录用户的请求
		boolean b=WebUtil.checkLoginUser(request);
		if(!b){
			//不是登录用户的请求，重定向到登录页
			Object o=request.getAttribute(WebUtil.INCLUDE_REQUEST);
			if(null!=o){
				//System.out.println("拦截admin.jsp而来");
				//终止当前的admin.jsp请求，转发到login.jsp请求
				request.getRequestDispatcher("login.jsp").forward(request, response);
			}else{	
				//直接请求
				response.sendRedirect("login.jsp");
			}
			
			return;
		}
		
		//为内部目录的admin.jsp的页面准备分页数据
		AdminService service=new AdminService();
		
		b=WebUtil.isOP(WebUtil.OP_ADD,request);
		if(b){//增加操作
			
		}
		b=WebUtil.isOP(WebUtil.OP_DEL,request);
		if(b){//删除操作
			
		}
		b=WebUtil.isOP(WebUtil.OP_UPDATE,request);
		if(b){//更新操作
			
		}
		//查询操作
		Integer pageSize=WebUtil.intParam(request, WebUtil.PAGE_SIZE, 3);
		Integer pageIndex=WebUtil.intParam(request, WebUtil.PAGE_INDEX, 1);
		Pager<News> pager=service.findNewsPager(pageSize,pageIndex);
		request.setAttribute(WebUtil.PAGER, pager);
		//将放在内部目录的admin.jsp包含进来
		request.getRequestDispatcher("WEB-INF/admin/admin.jsp").include(request, response);
		
		
	}

}
